<?php

class Admin_IndexController extends Vlib_Controller_Action {

   public function init() {
        Zend_Layout::getMvcInstance()->setLayout('login');
        $this->_logic = new Admin_Models_Logic();
        
        if($this->getRequest()->isXMLHttpRequest()) { //disable layout when ajax process 
           $this->_helper->layout()->disableLayout(); 
           $this->_helper->viewRenderer->setNoRender(true);
        }
        
        $this->view->headScript()->appendFile($this->view->baseUrl() . '/js/CLEditor1_3_0/jquery.cleditor.min.js');
        $this->view->headLink()->appendStylesheet($this->view->baseUrl() . '/js/CLEditor1_3_0/jquery.cleditor.css');
   }
    
   public function indexAction() {
        $this->_logger->debugIn();
        $intIsOk = Vlib_Constant::VLIB_OK;
        if (Zend_Auth::getInstance()->hasIdentity()) {
            //Login info
            $objLogin = Zend_Auth::getInstance()->getStorage()->read();
            //product
            $aryProduct = $this->_logic->getListProduct();
            
            //detail product
            $prdId = (int) $this->_request->getParam('prdId');
            $aryInfo = array();
            if($prdId > 0) {
                $aryFieldSelect = array('*');
                $intIsOk = $this->_logic->getOneProduct($aryFieldSelect,$prdId,$aryInfo);
            } else {
                $aryInfo['product_id'] = 0;
            }
            
            $this->view->objLogin = $objLogin;
            $this->view->aryProduct = $aryProduct;
            $this->view->aryInfo = $aryInfo;
        } else {
            $this->_helper->redirector('login','index','admin');
        }
        
        $this->_logger->debugOut();
   }
    
   public function loginAction() {
        $this->_logger->debugIn();
        $intIsOk = Vlib_Constant::VLIB_OK;
        // initialize Auth_Form object
        $authForm = new Admin_Forms_Login();
        // authentication if has post action occur
        if ($this->_request->isPost()) {
            // if post data is valid
            if ($authForm && $authForm->isValid($this->_request->getParams())) {
                $aryData = $authForm->getValues();
                // if user is login success
                if ($this->_logic->login($aryData)) {
                    $this->_helper->redirector('index','index','admin');
                } else {
                    $this->view->loginErr = $this->view->translate('_USERNAME_OR_PASSWORD_IS_INCORRECT');
                }
            } else {
                $arrMessage = $authForm->getMessages();
                if ($arrMessage['username']['isEmpty']) {
                    $this->view->loginErr = $arrMessage['username']['isEmpty'];
                } else if ($arrMessage['password']['isEmpty']) {
                    $this->view->loginErr = $arrMessage['password']['isEmpty'];
                }
                $authForm->setDefaults($this->_request->getParams());
            }
        }
        $this->_logger->debugOut();
   }
   
   public function logoutAction() {
        $this->_logger->debugIn();
        $intIsOk = Vlib_Constant::VLIB_OK;
        // clear current user data
        if (Zend_Auth::getInstance()->hasIdentity()) {
            Zend_Auth::getInstance()->clearIdentity();
            $_objSession = new Zend_Session_Namespace('Permission');
            $_objSession->unsetAll();
            // Delete all session data and cookie
            Zend_Session::destroy();
        }
        $this->_redirect($this->view->rootUrl . '/admin');
        
        $this->_logger->debugOut();
    }
    
    public function saveAction() {
        $this->_logger->debugIn();
        $intIsOk = Vlib_Constant::VLIB_OK;
        $aryParams = $this->_request->getParams();
        $prdId = $aryParams['product_id'];
        $form = new Admin_Forms_Product();
        if ($form->isValid($aryParams)) {
            
            //upload file 
            $upload = new Zend_File_Transfer_Adapter_Http();     
            $upload->setDestination(APPLICATION_PATH."/../public/upload/product");
            $upload->receive();
            
            $fileInfo = $upload->getFileInfo();
            $aryParams['product_image'] =  $fileInfo['product_image']['name'];
            
            unset($aryParams['module']);
            unset($aryParams['controller']);
            unset($aryParams['action']);
            unset($aryParams['product_id']);
            
            $aryParams['product_order'] = (int) $aryParams['product_order'];
            
            if($prdId > 0) { //edit products
                $intIsOk = $this->_logic->updateProduct($aryParams,$prdId);
            } else { //add product
                $intIsOk = $this->_logic->insertProduct($aryParams,$newId);
            }
        } else {
            $arrMessage = $form->getMessages();
            $arrMessage = $this->getErrorMessages($arrMessage);
            $intIsOk = Vlib_Constant::VLIB_ERR_DB_EXCEPTION;
        }    
        $aryData['error'] = $arrMessage;
        $aryData['intIsOk'] = $intIsOk;
        
        $this->_helper->layout()->disableLayout(); 
        $this->_helper->viewRenderer->setNoRender(true);
        $this->getResponse()->setBody(Zend_Json::encode($aryData));
        
        $this->_logger->debugOut();
    }
    
     public function getErrorMessages($input) {
        $this->_logger->debugIn();
        $intIsOk = Vlib_Constant::VLIB_OK;
        $output = '';
        if(is_array($input)) {
            foreach($input as $inp) :
                if(is_array($inp)) {
                    foreach($inp as $value) :
                        $output .= $value .'
';
                    endforeach;
                }
            endforeach;
        }
        
        $this->_logger->debugOut();
        return $output;
    }
    
    public function deleteAction() {
        $this->_logger->debugIn();
        $intIsOk = Vlib_Constant::VLIB_OK;
        $prdId = (int) $this->_request->getParam('prdId');
        if($prdId > 0) { //edit products
            $aryParams = array('product_delete_flag' => 1);
            $intIsOk = $this->_logic->updateProduct($aryParams,$prdId);
        }        
        $aryData = array();
        $aryData['intIsOk'] = $intIsOk;
        $this->getResponse()->setBody(Zend_Json::encode($aryData));
        $this->_logger->debugOut();
    }
    
    public function newsAction() {
        $this->_logger->debugIn();
        $intIsOk = Vlib_Constant::VLIB_OK;
        if (Zend_Auth::getInstance()->hasIdentity()) {
            //Login info
            $objLogin = Zend_Auth::getInstance()->getStorage()->read();
            //news
            $aryFieldSelect = array('news_id','news_title');
            $aryCondition = array('news_type' => 2); 
            $aryConfig = array();
            $order = "news_date DESC";
            $intIsOk = $this->_logic->getListNews($aryFieldSelect,$aryCondition,$aryConfig,$order,$aryListNews);

            //detail product
            $newsId = (int) $this->_request->getParam('newsId');
            $aryInfo = array();
            if($newsId > 0) {
                $aryFieldSelect = array('*');
                $intIsOk = $this->_logic->getOneNews($aryFieldSelect,$newsId,$aryInfo);
            } else {
                $aryInfo['news_id'] = 0;
            }
            
            $this->view->objLogin = $objLogin;
            $this->view->aryListNews = $aryListNews;
            $this->view->aryInfo = $aryInfo;
        } else {
            $this->_helper->redirector('login','index','admin');
        }
        
        $this->_logger->debugOut();
   }
   
   public function savenewsAction() {
        $this->_logger->debugIn();
        $intIsOk = Vlib_Constant::VLIB_OK;
        $aryParams = $this->_request->getParams();
        $aryParams['news_date'] = str_replace('/', '-', $aryParams['news_date']);
        $newsId = $aryParams['news_id'];
        $form = new Admin_Forms_News();
        if ($form->isValid($aryParams)) {
            
            //upload file 
            $upload = new Zend_File_Transfer_Adapter_Http();     
            $upload->setDestination(APPLICATION_PATH."/../public/upload/news");
            $upload->receive();
            
            $fileInfo = $upload->getFileInfo();
            $aryParams['news_image'] =  $fileInfo['news_image']['name'];
            
            unset($aryParams['module']);
            unset($aryParams['controller']);
            unset($aryParams['action']);
            unset($aryParams['news_id']);

            if($newsId > 0) { //edit news
                $intIsOk = $this->_logic->updateNews($aryParams,$newsId);
            } else { //add news
                $intIsOk = $this->_logic->insertNews($aryParams,$newId);
            }
        } else {
            $arrMessage = $form->getMessages();
            $arrMessage = $this->getErrorMessages($arrMessage);
            $intIsOk = Vlib_Constant::VLIB_ERR_DB_EXCEPTION;
        }    
        $aryData['error'] = $arrMessage;
        $aryData['intIsOk'] = $intIsOk;
        
        $this->_helper->layout()->disableLayout(); 
        $this->_helper->viewRenderer->setNoRender(true);
        $this->getResponse()->setBody(Zend_Json::encode($aryData));
        
        $this->_logger->debugOut();
    }
    
    public function deletenewsAction() {
        $this->_logger->debugIn();
        $intIsOk = Vlib_Constant::VLIB_OK;
        $id = (int) $this->_request->getParam('newsId');
        if($id > 0) { //del news
            $aryParams = array('news_delete_flag' => 1);
            $intIsOk = $this->_logic->updateNews($aryParams,$id);
        }        
        $aryData = array();
        $aryData['intIsOk'] = $intIsOk;
        $this->getResponse()->setBody(Zend_Json::encode($aryData));
        $this->_logger->debugOut();
    }
        
}

